
if(!isFunction(WheeledVehicleData, onImpact)) eval("function WheeledVehicleData::onImpact(){}");
if(!isFunction(FlyingVehicleData , onImpact)) eval("function FlyingVehicleData::onImpact (){}");

package RedBase_Impact {
	function WheeledVehicleData::onImpact(%data, %vehicle, %a) {
		parent::onImpact(%data, %vehicle, %a);
		RedImpact_Impact(%vehicle);
	}
	function FlyingVehicleData::onImpact(%data, %vehicle, %a) {
		parent::onImpact(%data, %vehicle, %a);
		RedImpact_Impact(%vehicle);
	}
};
activatePackage(RedBase_Impact);

function RedImpact_DestroyVehicle(%vehicle) {
	if(%vehicle.destroyed) return;
	%data = %vehicle.getDatablock();
	%vehicle.setDamageLevel(%data.maxDamage);
	%vehicle.destroyed = 1;
	%vehicle.schedule(%data.burnTime, finalExplosion);
	
	%mini = getMiniGameFromObject(%vehicle);
	if(isObject(%mini)) %respawnTime = %mini.vehicleRespawnTime;
	else %respawnTime = 5000;
	%vehicle.spawnBrick.spawnVehicle(%respawnTime);
}

function RedImpact_Impact(%vehicle) {
	%data = %vehicle.getDatablock();
	if(%data.impactExplosionSpeed $= "") return;
	
	%speed = vectorLen(%vehicle.getVelocity());
	if(%speed < %data.impactExplosionSpeed) return;
	
	%proj = new Projectile() {
		datablock = %data.impactExplosionProjectile;
		initialPosition = %vehicle.getPosition();
		initialVelocity = %vehicle.getVelocity();
		client = %vehicle.lastDamageClient;
		sourceClient = %vehicle.lastDamageClient;
	};
	MissionCleanup.add(%proj);
	
	RedImpact_DestroyVehicle(%vehicle);
}
